/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import Bean.CekStokBarangCabang;
import Bean.CekStokBarangPusat;
import Bean.Pemesanan;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/KonfirmasiTerima"})
public class KonfirmasiTerima extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String id = request.getParameter("id_cab");
        String kode_brg = request.getParameter("kode_brg");
        String nama_brg = request.getParameter("nama_brg");
        String jmlh = request.getParameter("jmlh");
        String kode_kate = request.getParameter("kode_kate");
        String jmlh_stok = request.getParameter("stok");
        String kode_psn = request.getParameter("kode_pesan");
        out.println(id + " " + kode_brg + " " + nama_brg + " " + jmlh + " " + kode_kate + " " + jmlh_stok + " " + kode_psn);

        int a1 = Integer.parseInt(jmlh_stok);
        int a2 = Integer.parseInt(jmlh);
        int a = a1 - a2;
        String jum = String.valueOf(a);

        Bean.CekStokBarangCabang cab = new CekStokBarangCabang();
        cab.setId_cab(id);
        cab.setKodebarang(kode_brg);
        cab.setKodekategori(kode_kate);
        cab.setNamabarang(nama_brg);
        ResultSet rset = cab.cekKodeBarang(id);
        String kodebarang = null;
        while (rset.next()) {
            kodebarang = rset.getString("KODEBARANG");
        }
        if (kodebarang == null) {
            cab.setJmlh(jmlh);
            cab.tambahStokCabang();
        } else {
            cab.updateStokCabang(jmlh, kode_brg, id);
        }
        Bean.CekStokBarangPusat cap = new CekStokBarangPusat();
        if (a2 <= a1) {
            try {
                cap.updateStok(kode_brg, jum);
            } catch (SQLException ex) {
                Logger.getLogger(KonfirmasiTerima.class.getName()).log(Level.SEVERE, null, ex);
            }

            Bean.Pemesanan psn = new Pemesanan();
            try {
                psn.deletePesanan(kode_psn);
            } catch (SQLException ex) {
                Logger.getLogger(KonfirmasiTerima.class.getName()).log(Level.SEVERE, null, ex);
            }

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Status Tambah Stok Pusat Frogstone</title>");
            out.println("<link href=\"css/style_web.css\" rel=\"stylesheet\" type=\"text/css\"/>");
            out.println("</head>");
            out.println("<body id=\"home\">");
            out.println("<div id=\"wrapper\">");
            out.println("<div id=\"header\">");
            out.println("<div id=\"site_title\">");
            out.println("<a href=\"#\"></a>");
            out.println("</div>");
            out.println("<div id=\"header_kanan\">");
            out.println("</div>");
            out.println("<div id=\"content\">");
            out.println("<center>");
            out.println("<h2><b><font color=\"red\" face=\"Verdana\">PROSES KONFIRMASI BERHASIL....!!!</font></b></h2></br>");
            CekStokBarangPusat pusat = new CekStokBarangPusat();
            pusat.deleteStokBarangHabis();
            out.println("<form action=\"Admin/KonfirmasiPemesanan.jsp\"><input type=\"submit\" name=\"submit\" value=\"KEMBALI\"/></form>");
            out.println("</center>");
            out.println("</div>");
            out.println("</div>");
            out.println("</div>");
            out.println("</body>");
            out.println("</html>");
        } else if (a2 > a1) {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Status Tambah Stok Pusat Frogstone</title>");
            out.println("<link href=\"css/style_web.css\" rel=\"stylesheet\" type=\"text/css\"/>");
            out.println("</head>");
            out.println("<body id=\"home\">");
            out.println("<div id=\"wrapper\">");
            out.println("<div id=\"header\">");
            out.println("<div id=\"site_title\">");
            out.println("<a href=\"#\"></a>");
            out.println("</div>");
            out.println("<div id=\"header_kanan\">");
            out.println("</div>");
            out.println("<div id=\"content\">");
            out.println("<center>");
            out.println("<h2><b><font color=\"red\" face=\"Verdana\">PROSES KONFIRMASI TIDAK BERHASIL....!!!</font></b></h2></br>");
            out.println("<form action=\"Admin/KonfirmasiPemesanan.jsp\"><input type=\"submit\" name=\"submit\" value=\"KEMBALI\"/></form>");
            out.println("</center>");
            out.println("</div>");
            out.println("</div>");
            out.println("</div>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(KonfirmasiTerima.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(KonfirmasiTerima.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
